let ipUtil = module.exports = {};

ipUtil.toLong = function (ip) {
    let ipl = 0;
    ip.split('.').forEach(function (octet) {
        ipl <<= 8;
        ipl += parseInt(octet);
    });
    return (ipl >>> 0);
};

ipUtil.fromLong = function (ipl) {
    return ((ipl >>> 24) + '.' +
        (ipl >> 16 & 255) + '.' +
        (ipl >> 8 & 255) + '.' +
        (ipl & 255));
};

ipUtil.getIp = function (req) {
    let x_real_ip = req.headers['x-real-ip'];
    let x_forwarded_for_ip = req.headers['x-forwarded-for'];
    let remoteIp = req.socket.remoteAddress;
    remoteIp = remoteIp.replace("::ffff:", "");
    let ip = remoteIp;
    console.logw('ip', ip);
    if (x_real_ip) ip = x_real_ip;
    else if (x_forwarded_for_ip) ip = x_forwarded_for_ip;
    return ip;
};

